

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Core.Configuration;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Web.admin;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin.controls.sitesettings {
  public partial class general : SiteSettingsControl {

    #region Member Variables

    private string view = string.Empty;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        view = Utility.GetParameter("view");
        if (pnlSettings.Visible) {
          SetGeneralSettingsProperties();
          if (!Page.IsPostBack) {
            SetLoginRequirement();
            SetNumberOfItemsDropDown();
            SetThemeSelections();
            txtSiteLogo.Text = SiteSettings.SiteLogo;
            ddlCategoryItems.SelectedValue = SiteSettings.CatalogItems.ToString();
            ddlTheme.SelectedValue = SiteSettings.Theme;
            ddlLoginRequirement.SelectedValue = Enum.GetName(typeof(LoginRequirement), SiteSettings.LoginRequirement);
            txtSiteName.Text = SiteSettings.SiteName;
            txtTagLine.Text = SiteSettings.TagLine;
            chkRequireSsl.Checked = SiteSettings.RequireSsl;
            txtNewsFeedUrl.Text = SiteSettings.NewsFeedUrl;
            chkDisplayRetailPrice.Checked = SiteSettings.DisplayRetailPrice;
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(general).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        SiteSettings.SiteLogo = txtSiteLogo.Text;
        SiteSettings.Theme = ddlTheme.SelectedValue;
        SiteSettings.SiteName = txtSiteName.Text.Trim();
        SiteSettings.TagLine = txtTagLine.Text.Trim();
        SiteSettings.LoginRequirement = (LoginRequirement)Enum.Parse(typeof(LoginRequirement), ddlLoginRequirement.SelectedValue);
        SiteSettings.RequireSsl = chkRequireSsl.Checked;
        int catalogItems = 6;
        int.TryParse(ddlCategoryItems.SelectedValue, out catalogItems);
        SiteSettings.CatalogItems = catalogItems;
        SiteSettings.DisplayRetailPrice = chkDisplayRetailPrice.Checked;
        SiteSettings.NewsFeedUrl = txtNewsFeedUrl.Text;
        base.Save(SiteSettings);
      }
      catch (Exception ex) {
        Logger.Error(typeof(general).Name + ".btnSave_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the theme selections.
    /// </summary>
    private void SetThemeSelections() {
      string[] themes = Directory.GetDirectories(Server.MapPath("~/App_Themes"));
      string path = string.Empty;
      for (int i = 0; i < themes.Length; i++) {
        themes[i] = new DirectoryInfo(themes[i]).Name;
      }
      ddlTheme.DataSource = themes;
      ddlTheme.DataBind();
    }

    /// <summary>
    /// Sets the number of items drop down.
    /// </summary>
    private void SetNumberOfItemsDropDown() {
      for(int i = 1; i <= 100; i++) {
        ddlCategoryItems.Items.Add(new ListItem(i.ToString(), i.ToString()));
      }
    }

    /// <summary>
    /// Sets the login requirement.
    /// </summary>
    private void SetLoginRequirement() {
      string[] names = Enum.GetNames(typeof(LoginRequirement));
      ddlLoginRequirement.DataSource = names;
      ddlLoginRequirement.DataBind(); 
    }

    /// <summary>
    /// Sets the general settings properties.
    /// </summary>
    private void SetGeneralSettingsProperties() {
      this.Page.Title = LocalizationUtility.GetText("titleSiteSettingsGeneralLayout");
      pnlSettings.GroupingText = LocalizationUtility.GetText("pnlGeneralLayout");
      LocalizationUtility.AddLabelText(lblTheme);
      LocalizationUtility.AddButtonText(btnSave);
      LocalizationUtility.AddHoverHelp(hlThemeHelp);
      LocalizationUtility.AddLabelText(lblSiteLogo);
      LocalizationUtility.AddHoverHelp(hlSiteLogoHelp);
      LocalizationUtility.AddLabelText(lblSiteName);
      LocalizationUtility.AddHoverHelp(hlSiteNameHelp);
      LocalizationUtility.AddLabelText(lblLoginRequirement);
      LocalizationUtility.AddHoverHelp(hlLoginRequirementHelp);
      LocalizationUtility.AddLabelText(lblRequireSsl);
      LocalizationUtility.AddHoverHelp(hlRequireSslHelp);
      LocalizationUtility.AddLabelText(lblCategoryItems);
      LocalizationUtility.AddHoverHelp(hlCategoryItemsHelp);
      LocalizationUtility.AddLabelText(lblDisplayRetailPrice);
      LocalizationUtility.AddHoverHelp(hlDisplayRetailPriceHelp);
      LocalizationUtility.AddLabelText(lblTagLine);
      LocalizationUtility.AddHoverHelp(hlTagLineHelp);
      LocalizationUtility.AddLabelText(lblNewsFeedUrl);
      LocalizationUtility.AddHoverHelp(hlNewsFeedUrlHelp);
      hlImageSelector.NavigateUrl = string.Format("~/admin/imageselector.aspx?view=s&controlId={0}", txtSiteLogo.ClientID);
    }

    #endregion

    #endregion

  }
}